package com.soft.utils;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbcp.BasicDataSourceFactory;

import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;

/**
 * @author ruoxi
 * @create 2021-12-08 14:11
 */
public class DBUtil {
    private static Connection conn = null;
    private static DataSource ds = null;

    static {
        Properties properties = new Properties();
        try {
            properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
            ds = BasicDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取连接池对象
     * @return
     */
    public static Connection getConn() {
        try {
            conn = ds.getConnection();
            return conn;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return conn;
    }

    /**
     * 关闭增删改后的资源
     * @param pst
     * @param connection
     */
    public static void close(PreparedStatement pst, Connection connection) {
        close(null, pst, connection);
    }

    /**
     * 关闭查询后的资源
     * @param resultSet
     * @param pst
     * @param conn
     */
    public static void close(ResultSet resultSet, PreparedStatement pst, Connection conn) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                try {
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }

    /**
     * 增删改
     * @param sql
     * @param params
     * @return
     */
    public static int excute(String sql, Object... params) {
        Connection conn = null;
        PreparedStatement pst = null;
        try {
            conn = getConn();
            pst = conn.prepareStatement(sql);
            for (int i = 1; i <= params.length; i++) {
                pst.setObject(i, params[i - 1]);
            }
            return pst.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(pst, conn);
        }
        return 0;
    }

    /**
     * 查询
     * @param sql
     * @param type
     * @param params
     * @param <T>
     * @return
     */
    public static <T> List<T> query(String sql, Class<T> type, Object... params) {
        Connection conn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        ArrayList<T> list = new ArrayList<>();
        try {
            conn = getConn();
            pst = conn.prepareStatement(sql);
            for (int i = 1; i <= params.length; i++) {
                pst.setObject(i, params[i-1]);
            }
            rs = pst.executeQuery();
            
            while (rs.next()){  // 遍历结果
                //创建t对象
                T t = type.newInstance();  // 创建对象，目的是保存遍历的每一条记录。
                ResultSetMetaData rsm = rs.getMetaData(); //元数据
                HashMap<String,Object> hm = new HashMap<>();   // 创建集合，目的是，保存每个字段的数据，也就对应每一行记录。
                for (int i = 1; i <= rsm.getColumnCount(); i++) {
                    hm.put(rsm.getColumnName(i),rs.getObject(rsm.getColumnName(i)));
                }
                BeanUtils.populate(t,hm);
                list.add(t);
            }
            return list;
        } catch (Exception throwables) {
            throwables.printStackTrace();
        } finally {
            close(rs,pst,conn);
        }
        return list;
    }
}